package com.xj.api.bussiness.valuePriceConsultant.web;


import com.feilong.core.DatePattern;
import com.feilong.core.Validator;
import com.feilong.core.date.DateUtil;
import com.xj.api.base.web.BaseController;
import com.xj.api.bussiness.valuePriceConsultant.service.ITbValuePriceConsultantService;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.base.common.util.ObjectUtils;
import com.xj.common.bussiness.deviceInformation.entity.TbDeviceInformation;
import com.xj.common.bussiness.valuePriceConsultant.entity.TbValuePriceConsultant;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author Hmemb
 * @since 2018-12-03
 */
@RestController
@Slf4j
@RequestMapping("/valuePriceConsultant/")
public class TbValuePriceConsultantController extends BaseController {

	@Autowired
	private ITbValuePriceConsultantService valuePriceConsultantService;

	@PostMapping("/save")
	@ApiOperation(value = "新增", httpMethod = "POST", notes = "新增")
	public AbstractBean add(TbValuePriceConsultant tbvaluepriceconsultant){
		if(!valuePriceConsultantService.insertOrUpdate(tbvaluepriceconsultant)){
			return error();
		}
		return success();
	}

	@PostMapping("/update")
	@ApiOperation(value = "修改", httpMethod = "POST", notes = "新增")
	public AbstractBean update(TbValuePriceConsultant tbvaluepriceconsultant){
		if(!valuePriceConsultantService.updateById(tbvaluepriceconsultant)){
			return error();
		}
		return success();
	}

	@DeleteMapping("{id}/delete")
	@ApiOperation(value = "  删除", httpMethod = "POST", notes = " 删除")
    public AbstractBean delete(@PathVariable(required=true) Integer id) {
		if(!valuePriceConsultantService.deleteById(id)){
			return error();
		}
		return success();
    }
	@PostMapping("/saveValuePriceConsultant")
	@ApiOperation(value = "新增用户设备备案信息", httpMethod = "POST", notes = "新增")
	public AbstractBean saveValuePriceConsultant(@RequestBody Map<String,Object>map) throws Exception{
		addValuePriceConsultant(map);
		return success();
	}

	@Transactional(readOnly=false)
	public void  addValuePriceConsultant(Map<String,Object>map) throws Exception{

		log.info("前端获取的信息：{}",map.toString());

		String employmentTime = (String) map.get("employmentTime");
		if(Validator.isNotNullOrEmpty(employmentTime)){
			map.put("employmentTime", DateUtil.toDate(employmentTime, DatePattern.COMMON_DATE));
		}
		//新增设备备案信息
		TbValuePriceConsultant tbvaluepriceconsultant=(TbValuePriceConsultant)
				ObjectUtils.transforMapToObject(TbValuePriceConsultant.class,map);
		tbvaluepriceconsultant.setCreateDate(new Date());
		valuePriceConsultantService.insert(tbvaluepriceconsultant);
	}
}
